#!/bin/bash

# 测试Fluent Bit Parser配置的脚本

echo "=== 测试Fluent Bit Parser配置 ==="
echo ""

# 检查fluent-bit是否安装
if ! command -v fluent-bit &> /dev/null; then
    echo "警告: 未找到fluent-bit命令，请先安装Fluent Bit"
    echo "安装方法:"
    echo "  macOS: brew install fluent-bit"
    echo "  Ubuntu: apt-get install fluent-bit"
    echo "  CentOS: yum install fluent-bit"
    echo ""
fi

# 创建测试日志文件
echo "创建测试日志文件..."
cat > test-fluent.log << 'EOF'
192.168.1.100 - - [18/Sep/2025:14:30:25 +0800] "GET /api/users HTTP/1.1" 200 1024 "http://localhost:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" "-" /api/users 0.123 192.168.1.10:8080
10.0.0.1 - - [18/Sep/2025:14:30:26 +0800] "POST /api/auth/login HTTP/1.1" 200 512 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15" "-" /api/auth/login 0.456 10.0.0.10:9000
172.16.0.1 - - [18/Sep/2025:14:30:27 +0800] "GET /prod-api/monitor/job/list?pageNum=1&pageSize=10 HTTP/1.1" 200 1780 "http://10.130.25.203:8000/d3/monitor/job" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36" "-" /prod-api/monitor/job/list?pageNum=1&pageSize=10 0.012 10.130.25.203:8088
EOF

echo "测试日志文件已创建: test-fluent.log"
echo ""

# 创建测试配置文件
echo "创建测试配置文件..."
cat > test-fluent.conf << 'EOF'
[SERVICE]
    Flush         1
    Log_Level     info
    Daemon        off
    Parsers_File  parsers-optimized.conf

[INPUT]
    Name              tail
    Path              test-fluent.log
    Parser            nginx_optimized
    Tag               nginx.test
    Refresh_Interval  1

[FILTER]
    Name                record_modifier
    Match               nginx.test
    Record              test_mode true

[OUTPUT]
    Name                stdout
    Match               nginx.test
    Format              json_lines
EOF

echo "测试配置文件已创建: test-fluent.conf"
echo ""

# 如果fluent-bit可用，运行测试
if command -v fluent-bit &> /dev/null; then
    echo "运行Fluent Bit测试..."
    echo "按Ctrl+C停止测试"
    echo ""
    
    # 运行fluent-bit进行测试
    fluent-bit -c test-fluent.conf
else
    echo "由于fluent-bit未安装，无法运行实际测试"
    echo "但您可以手动验证parser配置："
    echo ""
    echo "1. 安装Fluent Bit"
    echo "2. 运行: fluent-bit -c test-fluent.conf"
    echo "3. 查看解析后的JSON输出"
fi

echo ""
echo "测试完成！"
